package com.bamanker.dailylove.controller;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.bamanker.dailylove.config.DailyLoveConfigure;
import com.bamanker.dailylove.domain.*;
import com.bamanker.dailylove.service.DataRemoteClient;
import com.bamanker.dailylove.service.WechatRequestClient;
import com.bamanker.dailylove.utils.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
public class PushDailyController {

    @Resource
    DataRemoteClient dataRemoteClient;

    @Resource
    WechatRequestClient wechatRequestClient;

    /**
     * 推送晚安
     *
     * @return
     */
    @GetMapping("/pushNight")
    public String pushNight() {

        ResultVo resultVo = ResultVo.initializeResultVo(DailyLoveConfigure.Open_ID,
                DailyLoveConfigure.Template_ID_Night,
                DailyLoveConfigure.Color_Top);

        TianXinReqParam param1 = new TianXinReqParam();
        param1.setKey(DailyLoveConfigure.TianXin_Key);
        param1.setCity(DailyLoveConfigure.City_ID);
        param1.setType("7");
        String weatherResp = dataRemoteClient.getWeather(param1);
        JSONObject weatherJson = JSONObject.parseObject(weatherResp).getJSONArray("result").getJSONObject(0).getJSONArray("list").getJSONObject(1);
        String city = JSONObject.parseObject(weatherResp).getJSONArray("result").getJSONObject(0).getString("area");
        Weather weather = weatherJson.toJavaObject(Weather.class);

        resultVo.setAttribute("tomorrow", new DataItem(weather.getDate() + " " + weather.getWeek(), DailyLoveConfigure.Color_tomorrow));
        resultVo.setAttribute("city", new DataItem(city, DailyLoveConfigure.Color_city));
        resultVo.setAttribute("weather", new DataItem(weather.getWeather(), DailyLoveConfigure.Color_weather));
        resultVo.setAttribute("min_temperature", new DataItem(weather.getLowest(), DailyLoveConfigure.Color_minTem));
        resultVo.setAttribute("max_temperature", new DataItem(weather.getHighest(), DailyLoveConfigure.Color_maxTem));
        resultVo.setAttribute("quality", new DataItem(weather.getVis(), DailyLoveConfigure.Color_quality));


        TianXinReqParam param2 = new TianXinReqParam();
        param2.setKey(DailyLoveConfigure.TianXin_Key);

        String tipsResp = dataRemoteClient.getTips(param2);
        String tips = JSONObject.parseObject(tipsResp).getJSONArray("result").getJSONObject(0).getString("content");
        resultVo.setAttribute("tips", new DataItem(tips, DailyLoveConfigure.Color_tips));

        String nightResp = dataRemoteClient.getNight(param2);
        String night = JSONObject.parseObject(nightResp).getJSONArray("result").getJSONObject(0).getString("content");
        resultVo.setAttribute("night", new DataItem(night, DailyLoveConfigure.Color_night));

        String rainbowResp = dataRemoteClient.getRainbow(param2);
        String rainbow = JSONObject.parseObject(rainbowResp).getJSONArray("result").getJSONObject(0).getString("content");
        resultVo.setAttribute("rainbow", new DataItem(rainbow, DailyLoveConfigure.Color_dailyCn));

//        String englishResp = dataRemoteClient.getDailyEnglish(param2);
//        String english = JSONObject.parseObject(englishResp).getJSONArray("result").getJSONObject(0).getString("en");
//        resultVo.setAttribute("daily_english_en", new DataItem(english, DailyLoveConfigure.Color_dailyEn));


        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate(weather.getDate()));
        String festival = chineseDate.getFestivals();
        String term = chineseDate.getTerm();
        resultVo.setAttribute("lunar", new DataItem(chineseDate.toString(), DailyLoveConfigure.Color_chineseDate));
        resultVo.setAttribute("festival", new DataItem(festival + " " + term, DailyLoveConfigure.Color_festival));


        int girlBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday);
        log.debug("gbir_day:{}", girlBirthdays);
        resultVo.setAttribute("gbir_day", new DataItem(girlBirthdays - 1 + "", DailyLoveConfigure.Color_gbir));

        int boyBirthdays = DataUtils.getChineseBirthdays(DailyLoveConfigure.Boy_Birthday);
        log.debug("bbir_day:{}", boyBirthdays);
        resultVo.setAttribute("bbir_day", new DataItem(boyBirthdays - 1 + "", DailyLoveConfigure.Color_bbir));

        int catBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Cat_Birthday);
        log.debug("cbir_day:{}", catBirthdays);
        resultVo.setAttribute("cbir_day", new DataItem(catBirthdays - 1 + "", DailyLoveConfigure.Color_cbir));

        String words = "普通的一天";

        if (DataUtils.getBirthdays(DailyLoveConfigure.Love_Day) == 1) {
            words = "明天是恋爱周年纪念日！永远爱你~mua";
        } else if ((DataUtils.getLoveDays(DailyLoveConfigure.Love_Day)) % 100 == 1) {
            words = "明天是恋爱百日纪念日！提前庆祝哦~";
        } else if (girlBirthdays == 1) {
            words = "明天是lili大宝贝的生日啦";
        } else if (boyBirthdays == 1) {
            words = "明天是ss的生日！别忘了哦~";
        } else if (catBirthdays == 1) {
            words = "明天是小离谱的生日！别忘了小鱼干！";
        }

        resultVo.setAttribute("words", new DataItem(words, DailyLoveConfigure.Color_remark));

        int loveDays = DataUtils.getLoveDays(DailyLoveConfigure.Love_Day);
        log.debug("love_day:{}", loveDays);
        resultVo.setAttribute("love_day", new DataItem(loveDays + 1 + "", DailyLoveConfigure.Color_loveDay));

        log.debug("resultVo:{}", resultVo);
        WechatTokenParam wechatTokenParam = new WechatTokenParam();
        wechatTokenParam.setAppid(DailyLoveConfigure.App_ID);
        wechatTokenParam.setSecret(DailyLoveConfigure.App_Secret);

        String accessTokenResp = wechatRequestClient.getAccessToken(wechatTokenParam);
        log.debug("accessTokenJson:{}", accessTokenResp);
        String token = JSONObject.parseObject(accessTokenResp).getString("access_token");
        String responseStr = wechatRequestClient.sendMsg(resultVo, token);
        return responseStr;

    }

    /**
     * 推送早安
     */
    @GetMapping("/pushMorning")
    public String pushMorning() {

        TianXinReqParam param1 = new TianXinReqParam();
        param1.setKey(DailyLoveConfigure.TianXin_Key);
        param1.setCity(DailyLoveConfigure.City_ID);
        param1.setType("1");
        String weatherResp = dataRemoteClient.getWeather(param1);

        Weather weather = JSONObject.parseObject(weatherResp).getJSONArray("result").getJSONObject(0).toJavaObject(Weather.class);


        ResultVo resultVo = ResultVo.initializeResultVo(DailyLoveConfigure.Open_ID,
                DailyLoveConfigure.Template_ID_Morning,
                DailyLoveConfigure.Color_Top);

        resultVo.setAttribute("now", new DataItem(weather.getDate() + " " + weather.getWeek(), DailyLoveConfigure.Color_Now));
        resultVo.setAttribute("city", new DataItem(weather.getArea(), DailyLoveConfigure.Color_city));
        resultVo.setAttribute("weather", new DataItem(weather.getWeather(), DailyLoveConfigure.Color_weather));
        resultVo.setAttribute("min_temperature", new DataItem(weather.getLowest(), DailyLoveConfigure.Color_minTem));
        resultVo.setAttribute("max_temperature", new DataItem(weather.getHighest(), DailyLoveConfigure.Color_maxTem));
        resultVo.setAttribute("quality", new DataItem(weather.getQuality(), DailyLoveConfigure.Color_quality));


        TianXinReqParam param2 = new TianXinReqParam();
        param2.setKey(DailyLoveConfigure.TianXin_Key);

//        String tipsResp = dataRemoteClient.getTips(param2);
//        String tips = JSONObject.parseObject(tipsResp).getJSONArray("result").getJSONObject(0).getString("content");
//        resultVo.setAttribute("tips", new DataItem(tips, DailyLoveConfigure.Color_tips));

        String morningResp = dataRemoteClient.getMorning(param2);
        String morning = JSONObject.parseObject(morningResp).getJSONArray("result").getJSONObject(0).getString("content");
        resultVo.setAttribute("morning", new DataItem(morning, DailyLoveConfigure.Color_morning));

        String rainbowResp = dataRemoteClient.getRainbow(param2);
        String rainbow = JSONObject.parseObject(rainbowResp).getJSONArray("result").getJSONObject(0).getString("content");
        resultVo.setAttribute("rainbow", new DataItem(rainbow, DailyLoveConfigure.Color_dailyCn));

//        String englishResp = dataRemoteClient.getDailyEnglish(param2);
//        String english = JSONObject.parseObject(englishResp).getJSONArray("result").getJSONObject(0).getString("en");
//        resultVo.setAttribute("daily_english_en", new DataItem(english, DailyLoveConfigure.Color_dailyEn));

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String currentTime = dateFormat.format(date);
        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate(currentTime));
        String festival = chineseDate.getFestivals();
        String term = chineseDate.getTerm();
        resultVo.setAttribute("lunar", new DataItem(chineseDate.toString(), DailyLoveConfigure.Color_chineseDate));
        resultVo.setAttribute("festival", new DataItem(festival + " " + term, DailyLoveConfigure.Color_festival));


        int girlBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday);
        log.debug("gbir_day:{}", girlBirthdays);
        resultVo.setAttribute("gbir_day", new DataItem(girlBirthdays + "", DailyLoveConfigure.Color_gbir));

        int boyBirthdays = DataUtils.getChineseBirthdays(DailyLoveConfigure.Boy_Birthday);
        log.debug("bbir_day:{}", boyBirthdays);
        resultVo.setAttribute("bbir_day", new DataItem(boyBirthdays + "", DailyLoveConfigure.Color_bbir));

        int catBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Cat_Birthday);
        log.debug("cbir_day:{}", catBirthdays);
        resultVo.setAttribute("cbir_day", new DataItem(catBirthdays + "", DailyLoveConfigure.Color_cbir));

        String words = "普通的一天";

        if (DataUtils.getBirthdays(DailyLoveConfigure.Love_Day) == 0) {
            words = "今天是恋爱周年纪念日！永远爱你~mua";
        } else if ((DataUtils.getLoveDays(DailyLoveConfigure.Love_Day)) % 100 == 0) {
            words = "今天是恋爱百日纪念日！永远爱你~";
        } else if (girlBirthdays == 0) {
            words = "今天是lili宝贝的生日！生日快乐哟~";
        } else if (boyBirthdays == 0) {
            words = "今天是ss的生日！别忘了好好爱他~";
        } else if (catBirthdays == 0) {
            words = "今天是小离谱的生日！别忘了小鱼干！";
        }

        resultVo.setAttribute("words", new DataItem(words, DailyLoveConfigure.Color_remark));

        int loveDays = DataUtils.getLoveDays(DailyLoveConfigure.Love_Day);
        log.debug("love_day:{}", loveDays);
        resultVo.setAttribute("love_day", new DataItem(loveDays + "", DailyLoveConfigure.Color_loveDay));

        log.debug("resultVo:{}", resultVo);
        WechatTokenParam wechatTokenParam = new WechatTokenParam();
        wechatTokenParam.setAppid(DailyLoveConfigure.App_ID);
        wechatTokenParam.setSecret(DailyLoveConfigure.App_Secret);

        String accessTokenResp = wechatRequestClient.getAccessToken(wechatTokenParam);
        log.debug("accessTokenJson:{}", accessTokenResp);
        String token = JSONObject.parseObject(accessTokenResp).getString("access_token");
        String responseStr = wechatRequestClient.sendMsg(resultVo, token);
        return responseStr;

    }

    /**
     * 打印 response log
     *
     * @param responseStr
     */
    private void printPushLog(String responseStr) {
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        String msgCode = jsonObject.getString("errcode");
        String msgContent = jsonObject.getString("errmsg");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("[ " + dateFormat.format(new Date()) + " ] : messageCode=" + msgCode + ",messageContent=" + msgContent);
    }


}
